
from sketchc import *
# ~ from numpy import linspace


window = Window2((1000, 800))



chair = Obj()
b1 = make_block(U3(0.4, 0.4, 0.05))     # 坐垫
b1.t.pos = U3(0, 0, -0.8)
b1.as_unit() >> chair
b = make_block(U3(0.05, 0.05, 0.4))     #椅子腿
b.pile(b1.box.anchor(U3.c5), anchor=U3.c1)
b.as_unit() >> chair
b.pile(b1.box.anchor(U3.c6), anchor=U3.c2)
b.as_unit() >> chair
b.pile(b1.box.anchor(U3.c7), anchor=U3.c3)
b.as_unit() >> chair
b.pile(b1.box.anchor(U3.c8), anchor=U3.c4)
b.as_unit() >> chair
b = make_block(U3(0.05, 0.05, 0.2)) # 椅背支撑
b.pile(b1.box.anchor(U3.c3), anchor=U3.c7)
b.as_unit() >> chair
b.pile(b1.box.anchor(U3.c4), anchor=U3.c8)
b.as_unit() >> chair
bl = b
b = make_block(U3(0.4, 0.05, 0.2))  # 椅背
b.pile(bl.box.anchor(U3.c4), anchor=U3.c8)
b.as_unit() >> chair

chair.box = Box(U3(1, 1, 1), chair)
chair.prog = prog2
chair.color = V3(1.0, 1.0, 1.0)
chair.u(window.ctx)

t_block = chair
 
# ~ part1 = make_block(U3(0.2, 0.2, 1))
# ~ part2 = make_block(U3(0.8, 0.2, 0.2))
# ~ part2.pile(part1.box.anchor(U3.bk + U3.up), U3.bk + U3.up)
# ~ t_block = Obj()
# ~ part1.as_unit() >> t_block
# ~ part2.as_unit() >> t_block
# ~ t_block.box = Box(U3(0.8, 0.2, 1), t_block)
# ~ t_block.prog = prog2
# ~ t_block.u(window.ctx)
# ~ t_block.color = V3(1.0, 1.0, 1.0)


sc = Obj()
sc.color = V3(1.0, 1.0, 1.0)
sc.prog = prog2
t1 = Turtle()
t2 = Turtle(U3(-3, 0, 0), vrot(U3.up, -pi / 2))
t3 = Turtle(U3(3, 0, 0), vrot(U3.up, pi / 2))
t2.connect(t1)
t3.connect(t1)
for i in range(50):
    o1 = t_block.copy()
    t2.pile(o1, anchor=U3.bk+U3.dn)
    o1.as_unit() >> sc
    o2 = t_block.copy()
    t3.pile(o2, anchor=U3.bk+U3.dn)
    o2.as_unit() >> sc
    t1.iwalk(U3(0, 1.2, 0))
sc.u(window.ctx)
sc >> window

sc2 = sc.copy()
sc2.t.walk(rot=vrot(U3.up, pi / 2))

sc3 = sc.copy()
sc3.t.walk(rot=vrot(U3.up, pi))

sc4 = sc.copy()
sc4.t.walk(rot=vrot(U3.up, -pi / 2))

sc.t.iwalk(pos=U3(0, 5, 0))
sc2.t.iwalk(pos=U3(0, 5, 0))
sc3.t.iwalk(pos=U3(0, 5, 0))
sc4.t.iwalk(pos=U3(0, 5, 0))

sc2 >> window
sc3 >> window
sc4 >> window


camera = Camera(U3(0, -10, 1))
camera >> window

walker = Walker(camera)
walker >> window

cube = make_block(U3(0.5, 0.5, 0.5))
cube.color = V3(1, 1, 1)
cube.prog = prog2
cube.u(window.ctx)
cube >> window

@id(lambda f:window.update_callback.append(f) or f)
def f(window, dt):
    cube.t.walk(rot=vrot(U3.c2, dt  * 2))


clock = pygame.time.Clock()
while True:
    dt = clock.tick(60)
    window.update(dt)
    for e in pygame.event.get():
        if e.type == pygame.QUIT:
            exit()
        window.dispatch(e)
    window.render()   # screen 操作需要写在window的槽里
    pygame.display.flip()
            
